package com.example.apple.printingroommanagementsystem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Formatter;

/**
 * Created by apple on 17/2/24.
 */

public class OneInterfaceActivity extends FragmentActivity {
    private FrameLayout frame_one;
    private RadioGroup group_one;
    private ArrayList<Fragment> basePagers;
    private int position;
    SharedPreferences preferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        frame_one = (FrameLayout) findViewById(R.id.frame_one);
        group_one = (RadioGroup) findViewById(R.id.group_one);
        group_one.check(R.id.departmentList);
        basePagers = new ArrayList<>();
        basePagers.add(new departmentone());
        basePagers.add(new userone());
        basePagers.add(new homePageoneFormat());
        group_one.setOnCheckedChangeListener(new buttonClick());
        position = 0;
        setFormattert();
        judgeVersionNumber();
    }

    private void setFormattert() {
        //1、得到FramgmentManger
        FragmentManager manager = getSupportFragmentManager();
        //2、开启事务
        FragmentTransaction ft= manager.beginTransaction();
        //3、替换
        // ft.replace()
        ft.replace(R.id.frame_one,basePagers.get(position));
        //4、提交事务
        ft.commit();
    }



    public static String getVersionName(Context context) {
        return ObtainvVersion(context).versionName;
    }

    public static PackageInfo ObtainvVersion(Context context){
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

    private void installAPK(File t) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setDataAndType(
                Uri.fromFile(t),
                "application/vnd.android.package-archive");
        startActivity(intent);
    }
    public void judgeVersionNumber(){
        preferences =getSharedPreferences("ure", Activity.MODE_PRIVATE);
        final String token =preferences.getString("token","");
        final GetRequest get = new GetRequest();
        get.doGet("http://a.xl18z.cn/api/version","?token="+token,new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                JSONObject js = (JSONObject) msg.obj;
                JSONObject json;
                String version;
                try {
                    json =js.getJSONObject("record");
                    version  = json.getString("version");
                    Toast.makeText(getWindow().getContext(),"++"+version.toString(),Toast.LENGTH_SHORT).show();
                    String versionsd =String.valueOf(getVersionName(getWindow().getContext()));

                    if (!versionsd.equals(version.toString())){
                        Log.e("de-->",versionsd.toString());
                        final AlertDialog.Builder builder  = new AlertDialog.Builder(getWindow().getContext());
                        builder.setTitle("版本号不同，葫芦娃要给你更新");
//                        builder.setMessage("这是一个简单消息框" ) ;
//                        builder.setView(new ProgressBar(this));
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent= new Intent();
                                intent.setAction("android.intent.action.VIEW");
                                Uri content_url = Uri.parse("http://zzjapp.cn/shangHanLun.apk");
                                intent.setData(content_url);
                                startActivity(intent);
//                                Log.e("token--->",token.toString());
//                                File file = get.download("http://zzjapp.cn/shangHanLun.apk");
//                                System.out.println("---file:::" + file.getName() +"*****" + file.length());
//                                installAPK(file);
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                builder.create();
                            }
                        });
                        builder.show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private class buttonClick implements RadioGroup.OnCheckedChangeListener {


        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.departmentList:
                    position =0;
                    break;
                case R.id.userList:
                    position =1;
                    break;
                case R.id.homePageList:
                    position=2;
                    break;
            }
            setFormattert();
        }

    }
}
